package com.example.controller;

import com.example.businessexception.UserException;
import com.example.enums.ResultEnum;
import com.example.utils.ResultVO;
import com.example.utils.ResultVOUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 测试自定义业务异常
     *
     *  注意：自定义的异常通常都在service 层处理，此处为了演示，写在了controller中
     */
    @RequestMapping("/doLogin")
    public ResultVO login() {

        String username = "admin";
        String password = "123";
        if ("erq".equals(username)) {
            // 抛出一个 “用户名错误” 异常
            throw new UserException(ResultEnum.USER_NAME_ERROR);
        }

        try {
            if ("erq".equals(password)) {
                // 抛出一个 “用户名错误” 异常
                throw new UserException(ResultEnum.USER_NAME_ERROR);
            }
        } catch (Throwable e) {
            // 抛出一个 “密码错误” 异常
            throw new UserException(ResultEnum.USER_PASSWORD_ERROR);
        }
        return ResultVOUtil.success();
    }
}
